// pages/detail/detail.js
import lotteryService from '../../services/lottery';
import userService from '../../services/user';
import config from '../../config/api';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lottery:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const lotteryId = options.lottery_id; 
    lotteryService.detail(lotteryId)
    .then(res => {
      if (res.resCode == config.Lottery.ResCode.Success){
        const lottery = res.data;
        this.setData({
          lottery:lottery
        });
      }
    })
    .catch(err => console.error(err));
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    debugger;
    const path = '/pages/detail/detail?lottery_id=' + this.data.lottery.lottery_id;
    return {
      title: '分享一个抽奖活动给你',
      path: path,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  /**
   * 参加活动
   */
  participate:function(){
    debugger
    const lotteryId = this.data.lottery.lottery_id;
    const that = this;
    lotteryService.join(Number(lotteryId))
    .then(res => {
      if (res.resCode == config.Lottery.ResCode.Success){
        const userInfo = wx.getStorageSync('userInfo');
        let joinedUsers = that.data.lottery.joinedUsers;   
        lotteryService.detail(lotteryId)
          .then(res => {
            debugger;
            if (res.resCode == config.Lottery.ResCode.Success) {
              const lottery = res.data;
              this.setData({
                lottery: lottery
              });
            }
          })
      }else{
        wx.showToast({
          title: '参加失败',
          icon: 'none',
          duration: 2000
        });
      }
    })
    .catch(err => {
      wx.showToast({
        title: '参加失败',
        icon: 'none',
        duration: 2000
      });
    });
  },

  drawLottery:function(){
    //手动抽奖
    let lotteryId = this.data.lottery.lottery_id
    lotteryService.draw(lotteryId).then(res => {
      debugger;
      if (res.resCode == config.Lottery.ResCode.Success){
        lotteryService.detail(lotteryId)
        .then(res => {
          debugger;
          if (res.resCode == config.Lottery.ResCode.Success){
            const lottery = res.data;
            this.setData({
              lottery:lottery
            });
          }
        })
        .catch(err => console.error(err));        
      }
    }).catch(err => console.error(err));
  },
  gotoWinnerAddress:function(){
    wx.navigateTo({
      url: '../winnerAddress/winnerAddress?lottery_id=' + this.data.lottery.lottery_id
    })    
  },
  gotoAdd: function(){
    debugger;
    wx.switchTab({
      url: '../add/add'
    })     
  },
  gotoParticipate:function(){
    wx.setStorageSync('participateList', this.data.lottery.joinedUsers)
    wx.navigateTo({
      url: '../participate/participate'
    })     
  },
  gotoWinnerList:function(){
    wx.setStorageSync('winnerList', this.data.lottery.drawnUsers);
    wx.setStorageSync('lotteryName', this.data.lottery.items[0].name);
    wx.navigateTo({
      url: '../winnerList/winnerList'
    })     
  },
  gotoAddress:function(){
    wx.chooseAddress({
      success: function (res) {
        userService.address(res)
          .then(res => {
            console.log(res);
          })
          .catch(err => console.error(err));
      }
    });
  }
})